package com.uwo.house.controller;

import com.uwo.house.beans.SampleFilterEntity;
import com.uwo.house.service.ISampleFilterService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by cynic on 2017/12/5.
 */
@Controller
@RequestMapping("/filter")
public class SampleFilterController {
    @Autowired
    ISampleFilterService sampleFilterService;

    /**
     * 根据父级id获取筛选条件
     *
     * @param parentIndex
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getSampleFilters(@RequestParam("parentIndex") String parentIndex) {
        List<SampleFilterEntity> list = this.sampleFilterService.getSampleFilters(parentIndex);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 添加筛选条件
     *
     * @param sampleFilterEntity
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addSampleFilter(@RequestBody SampleFilterEntity sampleFilterEntity) {
        int result = this.sampleFilterService.addSampleFilter(sampleFilterEntity);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "添加成功");
        }
        return UtilReturn.resErrorMap("添加失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 删除筛选条件
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteSampleFilter(int id) {
        int result = this.sampleFilterService.deleteSampleFilter(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 编辑筛选条件
     *
     * @param sampleFilterEntity
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Map modifySampleFilter(@RequestBody SampleFilterEntity sampleFilterEntity) {
        int result = this.sampleFilterService.modifySampleFilter(sampleFilterEntity);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "修改成功");
        }
        return UtilReturn.resErrorMap("修改失败！", UtilReturn.ERROR_CODE);
    }

}
